<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('请假记录列表')" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li th:if="${isAdmin}">
                                <label>请假科室：</label>
                                <input type="text" name="deptName"/>
                            </li>
                            <li th:if="${isAdmin}">
                                <label>请假人员：</label>
                                <input type="text" name="userName"/>
                            </li>
                            <li>
                                <label>人员类型：</label>
                                <select name="userType">
                                    <option value="">所有</option>
                                    <option value="01">普通职工</option>
                                    <option value="02">中层及以上</option>
                                </select>
                            </li>
                            <li>
                                <label>休假类型：</label>
                                <select name="leaveType" th:with="type=${@dict.getType('record_leave_type')}">
                                    <option value="">所有</option>
                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                                </select>
                            </li>
                            <li>
                                <label>编制情况：</label>
                                <select name="workDuty" th:with="type=${@dict.getType('basic_work_duty')}">
                                    <option value="">所有</option>
                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                                </select>
                            </li>
                            <li class="select-time">
                                <label>创建时间： </label>
                                <input type="text" class="time-input" id="startTime" placeholder="开始时间" name="params[beginTime]"/>
                                <span>-</span>
                                <input type="text" class="time-input" id="endTime" placeholder="结束时间" name="params[endTime]"/>
                            </li>
                            <li class="select-time">
                                <label>休假时间： </label>
                                <input type="text" class="time-input" id="startTime1" placeholder="开始时间" name="params[beginTime1]"/>
                                <span>-</span>
                                <input type="text" class="time-input" id="endTime1" placeholder="结束时间" name="params[endTime1]"/>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="record:leave:add">
                    <i class="fa fa-plus"></i> 添加
                </a>
                <a class="btn btn-primary single disabled" onclick="edit()" shiro:hasPermission="record:leave:edit">
                    <i class="fa fa-edit"></i> 修改
                </a>
                <a class="btn btn-danger multiple disabled" onclick="remove()" shiro:hasPermission="record:leave:remove">
                    <i class="fa fa-remove"></i> 删除
                </a>
                <a class="btn btn-warning single disabled" onclick="submit()" shiro:hasPermission="record:leave:submit">
                    <i class="fa fa-print"></i> 提交
                </a>
                <a class="btn btn-warning single disabled" onclick="print()" shiro:hasPermission="record:leave:print">
                    <i class="fa fa-print"></i> 预览
                </a>
                <a class="btn btn-warning single disabled" onclick="cancel()" shiro:hasPermission="record:leave:cancel">
                    <i class="fa fa-print"></i> 打印撤销
                </a>
                <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="record:leave:export">
                    <i class="fa fa-download"></i> 导出
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var leaveTypeDatas = [[${@dict.getType('record_leave_type')}]];
        var workDutyDatas = [[${@dict.getType('basic_work_duty')}]];
        var prefix = ctx + "record/leave";

        var isAdmin = [[${isAdmin}]];

        $(function() {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                sortName: "createTime",
                sortOrder: "desc",
                modalName: "请假记录",
                columns: [{
                    checkbox: true
                },
                {
                    field: 'leaveId',
                    title: 'id',
                    visible: false
                },
                {
                    field: 'userType',
                    title: '人员类型',
                    formatter: function(value, row, index) {
                        if(value == '01') {
                            return "普通职工";
                        }else {
                            return "中层及以上";
                        }
                    }
                },
                {
                    field: 'userName',
                    title: '请假人'
                },
                {
                    field: 'agentUser',
                    title: '代理人'
                },
                {
                    field: 'leaveType',
                    title: '休假类型',
                    sortable: true,
                    formatter: function(value, row, index) {
                        return $.table.selectDictLabel(leaveTypeDatas, value);
                    }
                },
                {
                    field: 'workDuty',
                    title: '编制情况',
                    formatter: function(value, row, index) {
                        return $.table.selectDictLabel(workDutyDatas, value);
                    }
                },
                {
                    field: 'leaveContent',
                    title: '请假事由',
                    formatter: function (value, row, index) {
                        return $.table.tooltip(value);
                    }
                },
                {
                    field: 'startTime',
                    title: '开始时间',
                    sortable: true
                },
                {
                    field: 'endTime',
                    title: '结束时间',
                    sortable: true
                },
                {
                    field: 'days',
                    title: '天数',
                    sortable: true
                },
                {
                    field: 'verifyFlag',
                    title: '审核标识',
                    formatter: function(value, row, index) {
                        if(value == '0') {
                            return '未审核';
                        }else if (value == '1') {
                            return '待审核';
                        }else if (value == '2') {
                            return '已审核'
                        }
                    }
                },
                {
                    field: 'printFlag',
                    title: '打印标识',
                    formatter: function(value, row, index) {
                        if(value == '0') {
                            return '未打印';
                        }else {
                            return '已打印';
                        }
                    }
                },
                {
                    field: 'deptName',
                    title: '所属科室'
                },
                {
                    field: 'createTime',
                    title: '创建时间'
                },
                {
                    field: 'remark',
                    title: '备注'
                }]
            };
            $.table.init(options);
        });

        function edit(){
            var row = $("#bootstrap-table").bootstrapTable('getSelections')[0];
            if(!isAdmin){
                if(row.printFlag == '1'){
                    $.modal.alertError("已打印的请假单不能修改");
                    return;
                }
                if(row.verifyFlag == '2'){
                    $.modal.alertError("已审核的请假单不能修改");
                    return;
                }
            }
            $.operate.edit();
        }

        function remove(){
            var row = $("#bootstrap-table").bootstrapTable('getSelections')[0];
            if(!isAdmin) {
                if (row.printFlag == '1') {
                    $.modal.alertError("已打印的请假单不能删除");
                    return;
                }
                if (row.verifyFlag == '2') {
                    $.modal.alertError("已审核的请假单不能删除");
                    return;
                }
            }
            $.operate.removeAll();
        }

        function submit(){
            var row = $("#bootstrap-table").bootstrapTable('getSelections')[0];
            if(!isAdmin) {
                if (row.printFlag == '1') {
                    $.modal.alertError("已打印的请假单不能提交审批");
                    return;
                }
                if (row.verifyFlag == '2') {
                    $.modal.alertError("已审核的请假单不能再次提交");
                    return;
                }
            }
            var url = prefix + "/submit/" + row.leaveId;
            $.modal.openFull("提交请假审核", url);
        }

        function print(){
            var row = $("#bootstrap-table").bootstrapTable('getSelections')[0];
            if(!isAdmin) {
                if (row.printFlag == '1') {
                    $.modal.alertError("当前请假单已打印，不能重复打印");
                    return;
                }
            }
            var options = {
                title: "打印请假单",
                width: "800",
                height: "600",
                url: prefix + '/print/' + row.leaveId,
                skin: 'layui-layer-gray',
                btn: ['关闭'],
                yes: function (index, layero) {
                    layer.close(index);
                }
            };
            $.modal.openOptions(options);
        }

        /**
         * 撤销打印
         */
        function cancel() {
            var row = $("#bootstrap-table").bootstrapTable('getSelections')[0];
            if(isAdmin) {
                if (row.printFlag == '0') {
                    $.modal.alertError("当前请假单未打印，不能撤销打印");
                    return;
                }
                $.ajax({
                    type: "post",
                    url: prefix + "/cancel",
                    dataType: "json",
                    data: {"leaveId" : row.leaveId},
                    success: function(r) {
                        if (r.code == web_status.SUCCESS) {
                            $.modal.msgSuccess("撤销成功");
                            $.table.refresh();
                        } else {
                            $.modal.msgError(r.msg);
                        }
                        $.modal.closeLoading();
                    }
                });
            }
        }
    </script>
</body>
</html>